﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using SQLEditor.admin.service;
using SQLEditor.dal;
namespace SQLEditor.admin.ashx
{
    /// <summary>
    /// rm_department 的摘要说明
    /// </summary>
    public class rm_department : rm_base
    {
        DepartmentService s = new DepartmentService();
        public override void getall(HttpContext context)
        {
            string json = s.getall();
            context.Response.Write(json);
        }

        public override void search(HttpContext context)
        {
            string strWhere = "1=1";
            string sort = context.Request.Params["sort"] ?? "id";
            string order = context.Request.Params["order"] ?? "asc";
            int pageIndex = int.Parse(context.Request.Params["page"] ?? "1");
            int pageSize = int.Parse(context.Request.Params["rows"] ?? "20");

            string json = s.search(pageSize, pageIndex, sort + " " + order, strWhere);
            context.Response.Write(json);

        }

        public override void add(HttpContext context)
        {
            string name = context.Request.Params["ui_department_name_edit"] ?? "";
            string code = context.Request.Params["ui_department_code_edit"] ?? "";
            string sort = context.Request.Params["ui_department_sort_edit"];
            if (string.IsNullOrEmpty(sort)) sort = "10";
            string strParentId = context.Request.Params["ui_department_parentid_edit"];
            if (string.IsNullOrEmpty(strParentId)) strParentId = "0";
            int parentId = int.Parse(strParentId);
            
            
            tbDepartment b = new tbDepartment
            {
                name = name,
                code = code,
                sort = int.Parse(sort),
                parentId = parentId,
                addDate = DateTime.Now
            };
            string json = s.add(b);
            context.Response.Write(json);

        }
        public override void edit(HttpContext context)
        {
            int id = Convert.ToInt32(context.Request.Params["ui_department_id_edit"] ?? "0");
            string name = context.Request.Params["ui_department_name_edit"] ?? "";
            string code = context.Request.Params["ui_department_code_edit"] ?? "";
            string sort = context.Request.Params["ui_department_sort_edit"] ?? "";
            string strParentId = context.Request.Params["ui_department_parentid_edit"];
            if (string.IsNullOrEmpty(strParentId)) strParentId = "0";
            int parentId = int.Parse(strParentId);
            if (string.IsNullOrEmpty(sort)) sort = "10";

            tbDepartment b = new tbDepartment
            {
                id= id,
                name = name,
                code = code,
                sort = int.Parse(sort),
                parentId = parentId,
                addDate = DateTime.Now
            };
            string json = s.edit(b);
            context.Response.Write(json);
        }

        public override void delete(HttpContext context)
        {
            string ids = context.Request.Params["ids"];
            string json = s.delete(ids);
            context.Response.Write(json);
        }

        public override void other(HttpContext context)
        {
            string action = context.Request.Params["action"];
            if ("searchDepartmentUser" == action)
            {
                string deptId = context.Request.Params["departmentId"];
                string json = s.getUserByDeptId(int.Parse(deptId));
                context.Response.Write(json);
            }
        }
    }
}